﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PlutoStudio
{
    public class DisposeAction:IDisposable
    {
        public static readonly DisposeAction Empty = new DisposeAction(null);

        private Action _action;

        /// <summary>
        /// 创建一个 <see cref="DisposeAction"/> 对象的新实例。
        /// </summary>
        /// <param name="action">当前对象实例释放时执行的动作。</param>
        public DisposeAction( Action action)
        {
            _action = action;
        }

        public void Dispose()
        {
            // Interlocked prevents multiple execution of the _action.
            var action = Interlocked.Exchange(ref _action, null);
            action?.Invoke();
        }
    }
}
